<template>
	<view class="test-page">
		<web-view v-if="showWebview" id="webview_id" :src="src" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	import {
		getByOrdersId
	} from '@/pages/api/report/orderp.js'
	import {
		getWorkOrderLogs,
		getOrderDetail
	} from '@/pages/api/orderp/orderp.js'
		mapState,
		mapMutations
	} from 'vuex'
	// const SRC = 'http://172.20.10.4:5500/index.html'
	const SRC = 'https://zhatu.deqing.gov.cn/createPdf/pageForm/index.html'
	export default {
		onLoad(options) {
			console.log(options)
			this.src = `${SRC}?`
			this.webViewContext = my.createWebViewContext('webview_id');
		},
		mounted() {
			console.log('传过来的数据: ', this.infoApprove, this.infoOrder, this.infoRecords)
			this.showWebview = true
			this.webViewContext = my.createWebViewContext('web-view-2');
			console.log('跳转参数',e)
			this.recordId = e.id
			this.taskId = e.idd
			this.getParams()
			this.timer = setInterval(() => {
				// 向webview发送信息
				this.webViewContext.postMessage({
					infoApprove: this.infoApprove,
					infoOrder: this.infoOrder,
					infoRecords: this.infoRecords,
					infoUser:this.userInfo
				});
			}, 200)
		},
		destroyed() {
			clearInterval(this.timer)
			this.timer = null
		},
		computed: {
			...mapState(['userInfo']),
			...mapState('mdApprove', ['infoApprove']),
			...mapState('mdOrder', ['infoOrder', 'infoRecords']),
		},
		data() {
			return {
				src: '',
				webViewContext: null,
				mapshow: true,
				test: {},
				recordId:'',
				taskId:'',
				finnishInfo:{
					infoApprove:{},//审核详情
					infoOrderp:{},//orderp详情
					infoRecords:{},//流转记录
				},
				showWebview: false,
				timer: null,
			}
		},
		methods: {
			...mapMutations('mdOrder', []),
			// webview向app发送信息
			handleMessage(evt) {
			handleMessage({
				detail
			}) {
				console.log('收到webview信息', detail)
				if (!(typeof(detail.data) == "object")) {
					detail.data = JSON.parse(detail.data)
				}
				uni.$emit('busSetRouteTxzPdf', detail.data)
				if (detail.closeMap) {
					uni.navigateBack()
				}
			},
			//获取参数
			 getParams(){
				//获取审核详情 
				getByOrdersId({
					ordersId: this.idd
				}).then(res => {
					this.finnishInfo.infoApprove = res
					//获取orderp详情
					  getOrderDetail({
						id: this.id
					}).then(res => {
						this.finnishInfo.infoOrderp = res
						//获取流转记录
						  getWorkOrderLogs({
							id: this.idd
						}).then(res => {
							this.finnishInfo.infoRecords = res
							console.log('结束页面传参',this.finnishInfo)
							this.webViewContext.postMessage(this.finnishInfo);
						})
					})
				})			
			}
			
		},
	}
</script>

<style lang="scss" scoped>
	.test-page {
		width: 100%;
		height: 100%;

		#webview_id {
			width: 100%;
			height: 100%;
		}
	}
</style>
